package com.zlsoft.web.sp;

import android.text.TextUtils;
import android.widget.Toast;

import com.google.zxing.common.StringUtils;
import com.zlsoft.web.app.ApplicationMobile;
import com.zlsoft.web.bean.AppConfigBean;
import com.zlsoft.web.constant.DynamicValue;
import com.zlsoft.web.utils.JsonUtil;

import java.io.IOException;

/**
 * @author - qqz
 * @date - 2021-12-14/15:18
 * @note -
 * ---------------------------------------------------------------------------------------------------------------------------------
 * @modified -
 * @date -
 * @note -
 */
public class ConfigPreference {
    /** 实例 */
    private static ConfigPreference sInstance;
    /** SP实例 */
    private SPUtil mSPInstance;


    /**
     * 私有化构造函数
     */
    private ConfigPreference() {
        mSPInstance = new SPUtil(ApplicationMobile.getInstance(), ConfigPreference.class.getSimpleName());
    }

    /**
     * 获取实例
     *
     * @return 实例
     */
    public static ConfigPreference getInstance() {
        if (sInstance == null) {
            sInstance = new ConfigPreference();
        }

        return sInstance;
    }

    /**
     * 是否启动A
     *
     * @param result i
     */
    public void setConfig(String result) throws IOException {
        try {
            AppConfigBean mBean = JsonUtil.stringToObject(result, AppConfigBean.class);
            DynamicValue.setWebPath(mBean.ip);
            DynamicValue.setWebToken(mBean.token);

            mSPInstance.putString("config", result);
        } catch (Exception e) {
        }
    }

    /**
     * @return yes - no
     */
    public String getConfig() throws IOException {
        String mConfig = mSPInstance.getString("config", "");
        if (!TextUtils.isEmpty(mConfig)) {
            AppConfigBean mBean = JsonUtil.stringToObject(mConfig, AppConfigBean.class);
            DynamicValue.setWebPath(mBean.ip);
            DynamicValue.setWebToken(mBean.token);
        } else {
            DynamicValue.setWebPath("");
            DynamicValue.setWebToken("");
        }
        return mConfig;
    }


}
